// Prevents additional console window on Windows in release, DO NOT REMOVE
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::sync::Mutex;
use tauri::{Manager, State};

#[derive(Default)]
struct StartupImagePath(Mutex<Option<String>>);

#[tauri::command]
fn get_startup_image_path(state: State<StartupImagePath>) -> Option<String> {
    state.0.lock().unwrap().clone()
}

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_opener::init())
        .plugin(tauri_plugin_fs::init())
        .manage(StartupImagePath::default())
        .setup(|app| {
            let args: Vec<String> = std::env::args().collect();
            for arg in args {
                if arg.ends_with(".csv") {
                    app.state::<StartupImagePath>()
                        .0
                        .lock()
                        .unwrap()
                        .replace(arg);
                }
            }
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![get_startup_image_path])
        .run(tauri::generate_context!())
        .expect("error while running csv application");
}
